local xingduntx = fk.CreateSkill {
    name = "xingduntx",
    anim_type = "offensive",
}

Fk:loadTranslationTable{
    ["xingduntx"] = "星盾突袭",
    [":xingduntx"] = "出牌阶段，你可以弃置一张装备牌（若你有<a href=':progoldsheild'>【振金盾牌】</a>，则改为弃置此牌），并对一名其他角色造成1点伤害。然后你摸一张牌并选择一项：1. 令其弃置一张手牌；2. 令你本回合内下一张【杀】或【决斗】伤害+1，然后本回合删除此项。3. 将该角色装备区的【振金盾牌】置入你的装备区。",
    ["xingduntx_discard"] = "令其弃置一张手牌",
    ["xingduntx_damage"] = "令你本回合内下一张【杀】或【决斗】伤害+1",
    ["xingduntx_get"] = "将其【振金盾牌】置入你的装备区",
    ["#xingduntx-discard"] = "星盾突袭：请弃置一张手牌",
    ["@@xingduntx-damage"] = "星盾突袭：请弃置一张装备牌",
    ["#guangdmj"] = "星盾突袭：你可以弃置一张装备牌并对一名其他角色造成1点伤害。然后你摸一张牌并进行选择。",
    ["@@xingduntx_damage-turn"] = "星盾突袭",
}

local U = require "packages.utility.utility"

xingduntx:addEffect("active", {
    prompt = "#guangdmj",
    min_target_num = 1,
    card_num = 1,
    can_use = function(self, player)
        -- 检查区域内是否有振金盾牌
        local has_shield = false
        for _, id in ipairs(player:getCardIds("he")) do
            local card = Fk:getCardById(id)
            if card.type == Card.TypeEquip then
                has_shield = true
            end
        end 
        return has_shield and not player:isNude()
    end,
    card_filter = function(self, player, to_select, selected)
        return not player:prohibitDiscard(to_select) and Fk:getCardById(to_select).type == Card.TypeEquip and #selected == 0
    end,
    target_filter = function(self, player, to_select, selected, cards)
        return #selected == 0 and to_select ~= player
    end,
    
    on_use = function(self, room, effect)
      local player = effect.from
      local target = effect.tos[1]
    local card = table.find(player:getCardIds("he"), function(id)
        return Fk:getCardById(id).name == "progoldsheild"
    end)
    if card then
        room:throwCard(card, xingduntx.name,player,player)
    else
        room:throwCard(effect.cards, xingduntx.name,player,player)
    end
      room:damage{
        from = player,
        to = target,
        damage = 1,
        skillName = xingduntx.name,
      }
      room:drawCards(player,1)
      local chioces = {}
      if not target:isNude() then
        table.insert(chioces, "xingduntx_discard")
      end
      if player:getMark("zhenghuo_used-turn") == 0 then
        table.insert(chioces, "xingduntx_damage")
      end
      if table.find(target:getEquipments(Card.SubtypeArmor), function(id)
        return Fk:getCardById(id).name == "progoldsheild"
      end) then
        table.insert(chioces, "xingduntx_get")
      end
      if #chioces == 0 then
        return
      end
      local choice = player.room:askToChoice(player, {
        choices = chioces,
        skill_name = xingduntx.name,
      })
      if choice == "xingduntx_discard" then
        room:askToDiscard(target, {
          min_num = 1,
          max_num = 1,
          prompt = "#xingduntx-discard",
          skill_name = xingduntx.name,
          cancelable = false,
        })
      elseif choice == "xingduntx_damage" then
        room:setPlayerMark(player, "zhenghuo_used-turn", 1)
        room:setPlayerMark(player, "@@xingduntx_damage-turn", 1)
      elseif choice == "xingduntx_get" then
        local cards = table.filter(target:getCardIds("he"), function(id)
            return Fk:getCardById(id).name == "progoldsheild"
        end)
        room:moveCardIntoEquip(player, cards, xingduntx.name, true, player)
      end
    end,
      
  })


  xingduntx:addEffect(fk.CardUsing, {
    is_delay_effect = true,
    mute = true,
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self.name) and target == player then
            return (data.card.trueName == "slash" or data.card.name == "duel") and player:getMark("@@xingduntx_damage-turn") == 1
        end
    end,
    
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:setPlayerMark(player, "@@xingduntx_damage-turn", 0)
        data.additionalDamage = (data.additionalDamage or 0) + 1
    end,
})

return xingduntx 